<template>
  <div class="finance-management">
    <div class="header">
      <h2>财务管理</h2>
      <div class="subtitle">管理和跟踪所有财务收支、状态和详情</div>
      <el-button type="primary" class="add-button" @click="handleAdd">添加记录</el-button>
    </div>
    
    <!-- 搜索过滤区域 -->
    <div class="filter-section">
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="date-picker"
      />
      <el-select v-model="selectedType" placeholder="收支类型" clearable>
        <el-option
          v-for="item in typeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-select v-model="selectedStatus" placeholder="审核状态" clearable>
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" @click="handleSearch">筛选</el-button>
    </div>

    <!-- 统计卡片区域 -->
    <div class="statistics-cards">
      <el-card class="stat-card">
        <div class="stat-title">总收入</div>
        <div class="stat-amount">¥1,248,650</div>
        <div class="stat-change increase">↑12% 较上月</div>
      </el-card>
      <el-card class="stat-card">
        <div class="stat-title">总支出</div>
        <div class="stat-amount">¥865,420</div>
        <div class="stat-change decrease">↓5% 较上月</div>
      </el-card>
      <el-card class="stat-card">
        <div class="stat-title">本月收入</div>
        <div class="stat-amount">¥142,800</div>
        <div class="stat-change increase">↑8% 较上月</div>
      </el-card>
      <el-card class="stat-card">
        <div class="stat-title">本月支出</div>
        <div class="stat-amount">¥95,600</div>
        <div class="stat-change decrease">↓3% 较上月</div>
      </el-card>
    </div>

    <!-- 财务记录表格 -->
    <el-table :data="financeList" stripe style="width: 100%" v-loading="loading">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="date" label="日期" width="120" />
      <el-table-column prop="code" label="单据编号" width="150" />
      <el-table-column prop="type" label="类型">
        <template #default="scope">
          <el-tag :type="scope.row.type === '收入' ? 'success' : 'danger'">
            {{ scope.row.type }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="amount" label="金额">
        <template #default="scope">
          <span :class="scope.row.type === '收入' ? 'income' : 'expense'">
            {{ scope.row.type === '收入' ? '+' : '-' }}¥{{ scope.row.amount }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="category" label="类别" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="approver" label="审批人" />
      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <el-button-group>
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <span class="total-info">Total {{ total }} {{ pageSize }}/page</span>
      <el-pagination
        v-model:current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        layout="prev, pager, next"
        @current-change="handlePageChange"
      />
    </div>

    <!-- 添加/编辑记录对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加记录' : '编辑记录'"
      width="500px"
    >
      <el-form :model="financeForm" label-width="100px">
        <el-form-item label="日期">
          <el-date-picker v-model="financeForm.date" type="date" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="financeForm.type" placeholder="选择类型">
            <el-option label="收入" value="收入" />
            <el-option label="支出" value="支出" />
          </el-select>
        </el-form-item>
        <el-form-item label="金额">
          <el-input-number v-model="financeForm.amount" :min="0" :precision="2" />
        </el-form-item>
        <el-form-item label="类别">
          <el-select v-model="financeForm.category" placeholder="选择类别">
            <el-option
              v-for="item in categoryOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="financeForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import type { DateModelType } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'

interface FinanceRecord {
  id: string
  date: string
  code: string
  type: '收入' | '支出'
  amount: number
  category: string
  status: string
  approver: string
  remark?: string
}

interface FinanceForm {
  date: DateModelType
  type: '收入' | '支出'
  amount: number
  category: string
  remark: string
}

// 状态和选项
const dateRange = ref<[DateModelType, DateModelType]>([null, null])
const selectedType = ref('')
const selectedStatus = ref('')
const currentPage = ref(1)
const pageSize = ref(5)
const total = ref(11)
const dialogVisible = ref(false)
const dialogType = ref<'add' | 'edit'>('add')
const loading = ref(false)

const typeOptions = [
  { value: 'income', label: '收入' },
  { value: 'expense', label: '支出' }
]

const statusOptions = [
  { value: 'pending', label: '待审核' },
  { value: 'approved', label: '已通过' },
  { value: 'rejected', label: '已拒绝' }
]

const categoryOptions = [
  { value: 'salary', label: '工资' },
  { value: 'bonus', label: '奖金' },
  { value: 'office', label: '办公费用' },
  { value: 'travel', label: '差旅费用' },
  { value: 'other', label: '其他' }
]

// 示例数据
const financeList = ref<FinanceRecord[]>([
  {
    id: '1',
    date: '2025-06-15',
    code: 'FIN-001',
    type: '收入',
    amount: 50000,
    category: '工资',
    status: '已通过',
    approver: '张三'
  },
  {
    id: '2',
    date: '2025-06-14',
    code: 'FIN-002',
    type: '支出',
    amount: 2000,
    category: '办公费用',
    status: '待审核',
    approver: '-'
  }
])

const financeForm = ref<FinanceForm>({
  date: null,
  type: '收入',
  amount: 0,
  category: '',
  remark: ''
})

// 方法
const getStatusType = (status: string) => {
  switch (status) {
    case '已通过':
      return 'success'
    case '待审核':
      return 'warning'
    case '已拒绝':
      return 'danger'
    default:
      return 'info'
  }
}

const handleSearch = () => {
  currentPage.value = 1
  // 实现搜索逻辑
}

const handlePageChange = (page: number) => {
  currentPage.value = page
  // 实现分页逻辑
}

const handleAdd = () => {
  dialogType.value = 'add'
  financeForm.value = {
    date: null,
    type: '收入',
    amount: 0,
    category: '',
    remark: ''
  }
  dialogVisible.value = true
}

const handleEdit = (row: FinanceRecord) => {
  dialogType.value = 'edit'
  financeForm.value = {
    date: row.date as unknown as DateModelType,
    type: row.type,
    amount: row.amount,
    category: row.category,
    remark: row.remark || ''
  }
  dialogVisible.value = true
}

const handleDelete = async (row: FinanceRecord) => {
  try {
    await ElMessageBox.confirm('确认删除该记录吗？', '提示', {
      type: 'warning'
    })
    ElMessage.success('删除成功')
    // 实现删除逻辑
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

const handleSave = async () => {
  try {
    // 实现保存逻辑
    ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
    dialogVisible.value = false
  } catch (error) {
    ElMessage.error(dialogType.value === 'add' ? '添加失败' : '更新失败')
  }
}
</script>

<style scoped>
.finance-management {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.subtitle {
  color: #666;
  margin-top: 8px;
}

.filter-section {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.date-picker {
  width: 300px;
}

.statistics-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  padding: 20px;
}

.stat-title {
  color: #666;
  font-size: 14px;
}

.stat-amount {
  font-size: 24px;
  font-weight: bold;
  margin: 10px 0;
}

.stat-change {
  font-size: 12px;
}

.increase {
  color: #67C23A;
}

.decrease {
  color: #F56C6C;
}

.income {
  color: #67C23A;
  font-weight: bold;
}

.expense {
  color: #F56C6C;
  font-weight: bold;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.total-info {
  color: #666;
}
</style> 